﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CentrifugalSealCalc
{
    public static class CsvParser
    {
        public static List<Impeller> ReadImpellers(string path)
        {
            List<Impeller> Impellers = new List<Impeller>();
            int count = 0;
            try
            {
                using (StreamReader readFile = new StreamReader(path))
                {
                    string line;
                    string[] row;

                    while ((line = readFile.ReadLine()) != null)
                    {
                        if (count == 0)
                        {
                            count++;
                            continue; // do not add header
                        }

                        row = line.Split(',');
                        Impeller xp = new Impeller();
                        if (row.Length > 0)
                            xp.Name = row[0];
                        if (row.Length > 1)
                            Double.TryParse(row[1], out xp.T);
                        if (row.Length > 2)
                            Double.TryParse(row[2], out xp.R1);
                        xp.R1 = xp.R1 / 1000.0;
                        if (row.Length > 3)
                            Double.TryParse(row[3], out xp.R2);
                        xp.R2 = xp.R2 / 1000.0;
                        Impellers.Add(xp);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return Impellers;
        }

        public static List<Expeller> ReadExpellers(string path)
        {
            List<Expeller> expellers = new List<Expeller>();
            int count = 0;
            try
            {
                using (StreamReader readFile = new StreamReader(path))
                {
                    string line;
                    string[] row;

                    while ((line = readFile.ReadLine()) != null)
                    {
                        if (count == 0)
                        {
                            count++;
                            continue; // do not add header
                        }

                        row = line.Split(',');
                        Expeller xp = new Expeller();
                        if (row.Length > 0)
                            xp.Name = row[0];
                        if (row.Length > 1)
                            Double.TryParse(row[1], out xp.T);
                        if (row.Length > 2)
                            Double.TryParse(row[2], out xp.R1);
                        xp.R1 = xp.R1 / 1000.0;
                        if (row.Length > 3)
                            Double.TryParse(row[3], out xp.R2);
                        xp.R2 = xp.R2 / 1000.0;
                        expellers.Add(xp);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return expellers;
        }
    }
}